package org.csu.newpetstoreback.controller.front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.csu.newpetstoreback.common.CommonResponse;
import org.csu.newpetstoreback.service.OrderService;
import org.csu.newpetstoreback.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/order/")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("orders")
    @ResponseBody
    public CommonResponse<List<OrderVO>> getOrders(){
        return orderService.getOrders();
    }

    @PostMapping("orders")
    @ResponseBody
    public CommonResponse<OrderVO> addOrder(@RequestBody OrderVO orderVO){

        //orderid我们自己生成，不要用户传
        int orderNum = orderService.getOrderNum();
        orderVO.setOrderId(orderNum + 1);

        return orderService.addOrder(orderVO);
    }

    @GetMapping("orders/{orderid}")
    @ResponseBody
    public CommonResponse<OrderVO> getOrderById(@PathVariable("orderid")int orderId){
        return orderService.getOrderById(orderId);
    }

    @PutMapping("orders/{orderid}")
    @ResponseBody
    public CommonResponse<OrderVO> setOrderById(@PathVariable("orderid")int orderId, @RequestBody OrderVO orderVO) {
//        OrderVO orderVO = JSON.toJavaObject(JSONObject.parseObject(orderVOString), OrderVO.class);

        return orderService.setOrderById(orderId, orderVO);
    }

    @DeleteMapping("orders/{orderid}")
    @ResponseBody
    public CommonResponse<OrderVO> deleteOrderById(@PathVariable("orderid")int orderId) {
        return orderService.deleteOrderById(orderId);
    }

    @GetMapping("orders/cancel/{orderid}")
    @ResponseBody
    public CommonResponse<OrderVO> cancelOrderById(@PathVariable("orderid")int orderId) {
        return orderService.cancelOrderById(orderId);
    }

    @GetMapping("orders/receive/{orderid}")
    @ResponseBody
    public CommonResponse<OrderVO> receiveOrderById(@PathVariable("orderid")int orderId) {
        return orderService.receiveOrderById(orderId);
    }
}
